﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataTransferObject;
namespace _5ChickDeal.Ajax
{
    public partial class FileUpload : System.Web.UI.Page
    {
        DataClassesDataContext db;
        private string chuoirandom()
        {
            Random rand = new Random();
            string kq = "";

            for (int i = 0; i < 10; i++)
            {
                char ch = (char)rand.Next(65, 90);
                kq += ch;
            }

            return kq;
        }


        private int getMaxHinhAnhID()
        {
            int id = 0;
            try
            {
                id = db.HinhAnhs.Max(d => d.id);
            }
            catch { }
            return id;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            db = new DataClassesDataContext();
            HttpPostedFile uploads = Request.Files["FileData"];

            string file = System.IO.Path.GetFileName(uploads.FileName);
            file = chuoirandom() + file;
            string fullpath = Server.MapPath("~/Resource/ImageDeals/") + file;

            try
            {

                uploads.SaveAs(fullpath);
                Image1.ImageUrl = "~/Resource/ImageDeals/" + file;
                Image2.ToolTip = file;

                HinhAnh ha = new HinhAnh();
                ha.madeal = null;
                ha.id = getMaxHinhAnhID() + 1;
                ha.ten = file;

                db.HinhAnhs.InsertOnSubmit(ha);
                db.SubmitChanges();

            }

            catch
            {



            }

        }
    }
}
